import React, { useState, useEffect, useRef } from 'react';
import { createModel } from 'hox';
import { message } from 'antd';
import {
  deleteSu,
  getFsuList,
  getCustomMessage,
  customParams,
  importConfig,
  getLog,
  commandHttpConnect,
  updateInfo,
  command,
} from '../service.js';
import { useRequest } from '@umijs/hooks';
import { useLocalStorageState, useAntdTable } from '@umijs/hooks';

class Field {
  customAllList = [];
  visible = false;
  field = {};
  customMessageData = {};
  messageResult = {};
  requestAndResponse = {};
  contextMenuVisible = false;
  contextMenuPosition = { left: 0, top: 0 };
  messageDetailItem = {};
  messageDetailVisible = false;
  keyword = '';
  resultVisible = false;
  signleData = {};
  suItemData = {};
  issueErrorResult = {};
}
function useModel() {
  const field = new Field();
  const [issueMessage, setIssueMessage] = useLocalStorageState('issue-message', {});

  const [state, dispatch] = useState({
    ...field,
  });
  const clear = () => {
    dispatch({ ...field });
  };

  const setState = payload => {
    dispatch(prevState => ({ ...prevState, ...payload }));
  };
  const { loading, run, data } = useRequest(getFsuList, {
    manual: true,
    onSuccess: (result, params) => {},
  });
  //更新厂家信息
  const updateInfoProps = useRequest(updateInfo, {
    manual: true,
    onSuccess: (res, params) => {
      if (res.result) {
        message.success('更新成功！');
        let row = localStorage.getItem('deviceType');
        run({ pageSize: 9999, currentPage: 1, fsuTypeId: row ? JSON.parse(row)?.id : '' });
      } else {
        message.error(res.msg);
      }
    },
  });
  //检测http
  const { run: detectionhttp } = useRequest(commandHttpConnect, {
    manual: true,
    onSuccess: (res, params) => {
      if (res.result) {
        message.success('检测成功!');
      } else {
        message.error(res.msg);
      }
    },
  });
  //单个删除设备
  const deleteSuProps = useRequest(deleteSu, {
    manual: true,
    onSuccess: (res, params) => {
      if (res.result) {
        message.success('删除成功!');
        let row = localStorage.getItem('deviceType');
        run({ pageSize: 9999, currentPage: 1, fsuTypeId: row ? JSON.parse(row)?.id : '' });
      } else {
        message.error(res.msg);
      }
    },
  });
  //获取全部自定义列表
  const customProps = useRequest(getCustomMessage, {
    manual: true,
    cacheKey: 'allCustomMessage',
    onSuccess: (res, params) => {
      if (res.result) {
        setState({ customAllList: res.data || [] });
      } else {
        message.error(res.msg);
      }
    },
  });
  //获取报文参数
  const customParamsProps = useRequest(customParams, {
    manual: true,
    onSuccess: (res, params) => {
      if (res.result) {
        setState({
          field: res.data || {},
        });
      }
    },
  });
  //导入电信配置
  const importConfigProps = useRequest(importConfig, {
    manual: true,
    onSuccess: (res, params) => {
      if (res.result) {
        message.success('导入成功！');
        let row = localStorage.getItem('deviceType');
        run({ pageSize: 9999, currentPage: 1, fsuTypeId: row ? JSON.parse(row)?.id : '' });
      } else {
        message.error(res.msg);
      }
    },
  });

  //获取占位符参数
  const getCustomParams = params => {
    customParamsProps.run(params);
  };
  //下发命令
  const { run: postCommand, loading: commandLoading } = useRequest(command, {
    manual: true,
    onSuccess: (res, params) => {
      if (res.result) {
        const data = res?.data || {};
        if (res?.errorCode === 30004) {
          message.warn(res?.msg);
        } else {
          message.success('下发成功!');
        }

        setIssueMessage({ ...data, suId: params[0].suId });
        setState({ issueErrorResult: {} });
      } else {
        message.error(res.msg);
        const data = res?.data || {};
        switch (res?.errorCode) {
          case 30008:
            break;
          case 30009:
            setIssueMessage({ ...data, suId: params[0].suId });
            setState({ issueErrorResult: data });
            break;
        }
      }
    },
  });

  return {
    run,
    setIssueMessage,
    getCustomParams,
    loading,
    state: {
      ...state,
      data: data?.data || [],
      importConfigProps,
    },
    clear,
    customProps,
    postCommand,
    setState,
    detectionhttp,
    issueMessage,
    deleteSuProps,
    updateInfoProps,
    commandLoading,
  };
}

export default createModel(useModel);
